package com.coffee.admin.config;

import cn.hutool.core.date.DateUtil;
import com.coffee.admin.filter.XssFilter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.convert.converter.Converter;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Nullable;
import java.util.Date;

/**
 * WebMVC的配置
 *
 * @author Xulg
 * Created in 2019-05-30 9:44
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    /**
     * 404页面配置
     * 500页面配置
     */
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                // 走的是页面路由接口，而非页面模板
                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
                ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
                factory.addErrorPages(error404Page, error500Page);
            }
        };
    }

    @Bean
    public CustomGlobalExceptionResolver globalHandlerExceptionResolver() {
        return new CustomGlobalExceptionResolver();
    }

    /**
     * 静态资源访问
     */
    @SuppressWarnings("all")
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!registry.hasMappingForPattern("/static/**")) {
            registry.addResourceHandler("/static/**")
                    .addResourceLocations("classpath:/static/");
        }
        if (!registry.hasMappingForPattern("swagger-ui.html")) {
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
        }
        if (!registry.hasMappingForPattern("/webjars/**")) {
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }

    /**
     * 过滤器
     */
    //@Bean
    public XssFilter xssFilter() {
        return new XssFilter();
    }


    /**
     * 配置XssFilter过滤器
     */
    //@Bean
    public FilterRegistrationBean filterDemo4Registration() {
        FilterRegistrationBean<XssFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(xssFilter());
        registration.addUrlPatterns("/*");
        registration.setName("xssFilter");
        // 优先级最高
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registration;
    }

    /**
     * 字符串转日期
     */
    @Bean
    public Converter<String, Date> dateConverter() {
        return new Converter<String, Date>() {
            @Override
            public Date convert(@Nullable String dateString) {
                if (StringUtils.isBlank(dateString)) {
                    return null;
                } else if (NumberUtils.isParsable(dateString)) {
                    // 时间戳
                    return new Date(Long.parseLong(dateString));
                } else {
                    // yyyy-MM-dd HH:mm:ss
                    // yyyy-MM-dd
                    // ......
                    return DateUtil.parse(dateString).toJdkDate();
                }
            }
        };
    }
}
